package com.zzptc.myapp1112.ui.dashboard;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.zzptc.myapp1112.Adapter.ContactAdapter;
import com.zzptc.myapp1112.Bean.Contact;
import com.zzptc.myapp1112.Bean.User;
import com.zzptc.myapp1112.ChatActivity;
import com.zzptc.myapp1112.R;

import org.litepal.LitePal;

import java.util.List;
import java.util.concurrent.Callable;

public class DashboardFragment extends Fragment {
    private ListView contactlv;
    List<User> userList;
    ContactAdapter adapter;


   

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
      
        View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
        initView(view);
        setListener();
      
        return view;
    }

    private void setListener() {
        contactlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SharedPreferences sp =getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor=sp.edit();
                User user=userList.get(position);
                editor.putInt("friendid",user.getId());
                editor.putString("friendname",user.getName());
                editor.apply();
                Intent it=new Intent(getContext(), ChatActivity.class);
                startActivity(it);

            }
        });
    }

    private void initView(View view) {
        userList= LitePal.findAll(User.class);
        contactlv=view.findViewById(R.id.contactlv);
        adapter=new ContactAdapter(getContext(),R.layout.contact_item,userList);
        contactlv.setAdapter(adapter);

    }
    }


